package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.example.entity.Collection;
import org.example.entity.History;
import org.example.utils.Result;
import org.example.service.CollectionService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "收藏管理")
@RequestMapping("/sc")
@CrossOrigin
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    //根据用户id,类型id和外键id添加收藏
    @PostMapping("/add-collection")
    public Result addCollection(@RequestParam("type") Integer type, @RequestParam("cid") Integer cid){

        Integer result = collectionService.addCollection(Math.toIntExact(ThreadLocalUtil.getCurrentId()),type,cid);
        System.out.println(result);
        if(result>0){
            return new  Result<>("200","收藏成功");
        }else {
            return new  Result<>("500","收藏失败");
        }
    }

    //根据id删除收藏
    @DeleteMapping("/delete-collection")
    public Result deleteCollection(@RequestParam("id") Integer id){
        boolean b = collectionService.removeById(id);
        if(b){
            return new  Result<>("200","删除成功");
        }else {
            return new  Result<>("500","删除失败");
        }
    }

    //根据用户id查看收藏列表
    @GetMapping("/collectionListByUid")
    public Result getCollectionList(){
        List<Object> list=new ArrayList<>();
        for (Collection collection : collectionService.list(new QueryWrapper<Collection>().eq("uid", ThreadLocalUtil.getCurrentId()))) {
            collection.setObj(collectionService.showList(collection.getType(),collection.getCid(), Math.toIntExact(ThreadLocalUtil.getCurrentId())));
            list.add(collection);
        }
        return new Result<>("200","查询成功",list);
    }
}
